<?php

App::uses('AppController', 'Controller');

/**
 * Materias Controller
 *
 * @property Materia $Materia
 */
class MateriasController extends AppController
{

    public $uses = array('Materia');
    public $layout = 'colegio';

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('*');
    }

    public function index()
    {
        $usuario = $this->Auth->user('id');
        //debug($usuario);
        $materias = $this->Materia->find('all', array('recursive' => -1));
        $this->set(compact('materias', 'usuario'));
        
    }

    function insertar(){
        if(!empty($this->data)){
            if($this->Materia->save($this->data)){
                $this->Session->setFlash('Se Guardo Correctamente!!!');
                $this->redirect(array('action'=>'index')); 
                }
            else{
                $this->Session->setFlash('Error al Guardar consulte con el Administrador de Sistema');
            }
        }
        
    }
    
    function editar($id=null){
        $this->Materia->id=$id;
        if(!$id){
            $this->Session->setFlash('No Existe el tipo de Materia');
            $this->redirect(array('action' =>'index'));
        }
        if(empty($this->data)){
            $this->data=$this->Materia->read();
        }
        else{
            if($this->Materia->save($this->data)){
                $this->Session->setFlash('Se Guardo Correctamente la Materia');
                $this->redirect(array('action'=>'index'));
                }
                else{
                    $this->Session->setFlash('Error al guardar la Materia');
                }
        }

    }
    function eliminar($id=null){
        $this->Materia->id=$id;
        $this->data=$this->Materia->read();
        if(!$id){
            $this->Session->setFlash('No existe la Materia a Eliminar');
            $this->redirect(array('action' =>'index'));
        }
        else{
            if($this->Materia->delete($id)){
                $this->Session->setFlash('Se elimino la Materia '.$this->data['Materia']['nombre']);
                $this->redirect(array('action' =>'index'));
            }
            else{
                $this->Session->setFlash('Error al eliminar');
            }
        }
    }
}
